package com.ys.everydaynews.ui.main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import com.ys.everydaynews.R
import com.ys.everydaynews.databinding.DialogTabListBinding
import com.ys.everydaynews.ui.main.adapter.ChannelAdapter
import com.ys.everydaynews.ui.main.model.TabListModel
import com.ys.everydaynews.utils.StatusBarUtils.setTranslucentStatus
import com.ys.everydaynews.view.ItemDragHelperListener

class TabListDialogFragment(mTabListModel: TabListModel) : DialogFragment() {

    private val mTabListModel: TabListModel = mTabListModel
    private var binding: DialogTabListBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.Dialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.dialog_tab_list,
            container,
            false
        )
        initView()
        return binding!!.root
    }

    private fun initView() {
        dialog!!.window!!.setWindowAnimations(R.style.dialogAnim)
        setTranslucentStatus(dialog!!)
        binding!!.imgOff.setOnClickListener { dismiss() }
        val layoutManager = GridLayoutManager(dialog!!.context, 4)
        val itemHelper=ItemDragHelperListener()
        val mItemTouchHelper=ItemTouchHelper(itemHelper)
        mItemTouchHelper.attachToRecyclerView(binding!!.recyTab)
        val adapter = ChannelAdapter(mTabListModel,mItemTouchHelper)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return when (adapter.getItemViewType(position)) {
                    0 -> 4
                    1 -> 4
                    2 -> 1
                    else -> 1
                }
            }

        }
        binding!!.recyTab.layoutManager = layoutManager
        binding!!.recyTab.adapter = adapter
    }
}